/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pressa.server.service;

/**
 *
 * @author Lucas Borri (lborri@pressa.com.ar)
 */
public class ACKandSaveReportThread extends Thread {

    private String packet;
    private String packetID;
    private String fmiPacketID;

    public ACKandSaveReportThread(String pckt) {
        packet = pckt;
        packetID = packet.substring(packet.indexOf(",") + 1, packet.indexOf(",") + 3);
        /* PacketID: 
         * 06 -> ACK
         * A1 -> Comando
         */
    }

    public void run() {
        //---Configuracion de la Base de datos en localhost
        MySQLService database = new MySQLService();
        database.configDB("garmintest", "donpedro", "donpedro");

        //---UDPService
        UDPService packetService = new UDPService();

        int reintento = 0;
        boolean flag = false;
        String ack = "";

        if (packetID.contains("06")) {
            System.out.println("Gamin -> Server: " + packet + "(ACK)");         //Es un ACK, no necesita ACK :P
        }
        if (packetID.contains("15")) {
            System.out.println("Gamin -> Server: " + packet + "(NAK)");         //Es un NAK, no necesita ACK :P
        }
        if (packetID.contains("A1")) {                                          //Es un FMI packet
            System.out.println("Garmin -> Server: " + packet + "(FMI Packet)");
            ack = ">SFM.,0601" + packetID + "<";
            fmiPacketID = packet.substring(packet.indexOf(",") + 5, packet.indexOf(",") + 9);
            flag = true;
            System.out.println("Server -> Garmin: " + ack + "(ACK)");
        }
        try {
            while (flag) {
                Thread.sleep(2000);
                packetService.sendPacket(ack, packet.substring(packet.indexOf("(") + 1, packet.indexOf(")"))); //Envio el ACK                   
                String received = packetService.receiveAckPacket("4004");       //Espero la respuesta
                if (received.contains("RFM.,READY") && !received.isEmpty()) {
                    System.out.println("Garmin -> Server: " + received + "(Trax ACK)");
                    if (database.insertPacket(packet)) {                          //Si se pudo insertar en la DB, responde el ACK
                        System.out.println("Server -> DataBase: " + packet);
                        //---FMIPacket 0x0026 Open Text Message
                        if (fmiPacketID.equals("2600")) {
                            int retry = 0;
                            String newAck = ">SFM.,A1062500"
                                    + packet.substring(packet.indexOf(",") + 33, packet.indexOf(",") + 41) + "<";
                            while (true) {
                                System.out.println("Server -> Garmin: " + newAck + "(0x0025 - Text Message Receipt)");
                                Thread.sleep(2000);
                                packetService.sendPacket(newAck, packet.substring(packet.indexOf("(") + 1, packet.indexOf(")"))); //Envio el ACK                      //Envio el ack
                                received = packetService.receiveAckPacket("4004");       //Espero la respuesta
                                if (received.contains("RFM.,READY") && !received.isEmpty()) {
                                    System.out.println("Garmin -> Server: " + received + "(Trax ACK)");
                                    break;
                                }
                                if (retry > 5) {
                                    System.out.println("Server -> Garmin: " + newAck + "(ACK)(Reintentos Agotados)");
                                }
                            }
                        }
                        //---
                        break;
                    } else {
                        System.out.println("ACK no válido:" + received);
                    }
                }
                Thread.sleep(1000);
                reintento++;
                if (reintento == 5) {
                    System.out.println("Server -> Garmin: " + packet + "(ACK)(Reintentos Agotados)");
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
